package com.guoqing.example.utils;

import lombok.extern.slf4j.Slf4j;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

@Slf4j
public final class PinYin4J {

    public static String getPinYinShort(String str) {
        StringBuilder pinyinStr = new StringBuilder();
        char[] newChar = str.toCharArray();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (char c : newChar) {
            if (c > 128) {
                try {
                    pinyinStr.append(PinyinHelper.toHanyuPinyinStringArray(c, defaultFormat)[0].charAt(0));
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    log.error("convert chinese to pinyin error:", e);
                }
            } else {
                pinyinStr.append(c);
            }
        }
        return pinyinStr.toString();
    }

    public static String getPinYinFull(String str) {
        StringBuilder pinyinStr = new StringBuilder();
        char[] newChar = str.toCharArray();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (char c : newChar) {
            if (c > 128) {
                try {
                    pinyinStr.append(PinyinHelper.toHanyuPinyinStringArray(c, defaultFormat) != null ? PinyinHelper.toHanyuPinyinStringArray(c, defaultFormat)[0] : "");
                } catch (Exception e) {
                    log.error("convert chinese to pinyin error:", e);
                }
            } else {
                pinyinStr.append(c);
            }
        }
        return pinyinStr.toString();
    }

    /**
     * 1.括号中的都不要  不管几个括号、中文 英文
     * 2.只保留中文
     *
     * @param str
     * @return
     */
    public static String getHanZi4Sku(String str) {
        str = str.replaceAll("（", "(");
        str = str.replaceAll("）", ")");
        StringBuffer sb = new StringBuffer(str);
        boolean isContinue = true;
        while (isContinue) {
            int start = sb.indexOf("(");
            int index = sb.indexOf(")");
            if (start == -1 && index != -1) {
                sb.delete(0, index + 1);
            } else if (start != -1 && index == -1) {
                sb.delete(start, sb.toString().length() + 1);
            } else if (start == -1 && index == -1) {
                isContinue = false;
            } else {
                sb.delete(start, index + 1);
            }
        }
        return sb.toString().replaceAll("[^\\u4e00-\\u9fa5]", "");
    }


    public static void main(String[] args) {
        System.out.println(getPinYinFull(getHanZi4Sku("汼肉风味小串（君利，10包/箱）-WXZY")));
        System.out.println(getPinYinFull("汼肉风味小串"));
        System.out.println(getPinYinFull(getHanZi4Sku("沙拉瓶-BJ")));
        System.out.println(getPinYinFull(getHanZi4Sku("手撕牛心菜片（手撕片，8-10CM，1KG/包")));
        System.out.println(getPinYinFull(getHanZi4Sku("(dhsak)手撕牛心菜片（手撕片，8-10CM，1KG/包）")));
    }
}
